package route

import (
	"fmt"
	"strconv"
	"zinxtcp/ziface"
)

var Route ziface.IRouter

func init() {
	Route = &Router{
		Apis: make(map[uint32]ziface.IHandler),
	}
}

type Router struct {
	Apis map[uint32]ziface.IHandler //存放每个MsgId 所对应的处理方法的map属性
}

func (r *Router) AddRouter(msgType uint32, handler ziface.IHandler) {
	//1 判断当前msg绑定的API处理方法是否已经存在
	if _, ok := r.Apis[msgType]; ok {
		panic("repeated api , msg type = " + strconv.Itoa(int(msgType)))
	}
	//2 添加msg与api的绑定关系
	r.Apis[msgType] = handler
	fmt.Println("Add api msg type = ", msgType)
}

func (r *Router) GetRouter(msgType uint32) ziface.IHandler {
	handler, ok := r.Apis[msgType]
	if !ok {
		fmt.Println("api msg type = ", msgType, " is not FOUND!")
		return nil
	}
	return handler
}
